package com.controller.xsxt;

import com.entity.common.ApiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.xsxt.ThisRaceService;
import com.entity.xsxt.ThisRace;
import com.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by System on 2019/04/12 10:54:54.
 */
@RestController
@RequestMapping("thisRace")
public class ThisRaceController {

    @Autowired
    private ThisRaceService thisRaceService;

    @RequestMapping("/getPageList")
    public Map<String,Object> getPageList(@RequestParam(value = "pager.pageNo",defaultValue ="1" )  Integer pageNum,
                @RequestParam(value = "pager.pageSize",defaultValue ="10" ) Integer pageSize,ThisRace thisRace){
        Map<String, Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<ThisRace> pageInfo = new PageInfo<>(thisRaceService.getPageList(thisRace));
        map.put("rows",pageInfo.getList());
        map.put("pager.totalRows", pageInfo.getTotal());
        map.put("pager.pageNo",pageNum);
        return  map;
    }

    @RequestMapping("/getAllData")
    public List<ThisRace> getAllData(){
        ThisRace thisRace=new ThisRace();
        thisRace.setEndTime(new Date());
        return thisRaceService.getPageList(thisRace);
    }

    @RequestMapping("/addThisRace")
    public ApiResult addThisRace(ThisRace thisRace, MultipartFile file,HttpServletRequest request){
        if(file!=null){
            thisRace.setFilePath(FileUploadUtil.savaFile(file,request));
        }
        return thisRaceService.addThisRace(thisRace);
    }

    @RequestMapping("/deleteThisRace")
    public ApiResult deleteThisRace(Long id){
        return thisRaceService.deleteThisRace(id);
    }

    @RequestMapping("/getThisRaceById")
    public ApiResult getThisRaceById(Long id){
        return thisRaceService.getThisRaceById(id);
    }

    @RequestMapping("/editThisRace")
    public ApiResult editThisRace(ThisRace thisRace, MultipartFile file,HttpServletRequest request,String deleteFile){
        if("1".equals(deleteFile)){
            thisRace.setFilePath(null);
        }
        if(file!=null){
            thisRace.setFilePath(FileUploadUtil.savaFile(file,request));
        }
        return thisRaceService.editThisRace(thisRace);
    }

}
